package com.alameda.core;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 运行信息
 *
 * @author Lv
 * @date 2024/2/19
 */
@SuppressWarnings("all")
@Slf4j
public class RunMessage {
    /**
     * 私有构造
     */
    private RunMessage() {
    }

    /**
     * 定义对象
     */
    private static RunMessage runMessage;

    /**
     * 单例模式(懒汉式)获取实例
     *
     * @return
     */
    public static RunMessage getInstance() {
        // 先判空 是否需要实例化
        if (runMessage == null) {
            // 锁住当前对象 保证线程安全
            synchronized (RunMessage.class) {
                // 采用双重检查锁机制 降低锁的粒度
                if (runMessage == null) {
                    runMessage = new RunMessage();
                }
            }
        }
        return runMessage;
    }

    /**
     * 获取运行相关信息
     *
     * @param configurableApplicationContext 启动运行配置对象
     */
    public void getMessage(ConfigurableApplicationContext configurableApplicationContext) {
        Environment environment = configurableApplicationContext.getEnvironment();
        // 输出当前环境新
        String activeProfile = getCurrentProfile(environment);

        String ip = null;
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        printSuccess();
        // 获取服务端口
        String port = environment.getProperty("server.port");

        log.info("\n\t----------------------------------------------------------\n\t" +
                "项目启动成功         当前运行环境" + activeProfile + "!         调试地址:\n\t" +
                "knife4j-doc : \t\thttp://" + ip + ":" + port + "/doc.html\n\t" +
                "generator   : \t\thttp://" + ip + ":" + port + "/dist/index.html\n\t" +
                "----------------------------------------------------------");
    }

    /**
     * 打印启动成功标识
     */
    private void printSuccess() {
        log.info("\n" +
                "                           _ooOoo_\n" +
                "                          o8888888o\n" +
                "                          88\" . \"88\n" +
                "                          (| -_- |)\n" +
                "                          O\\  =  /O\n" +
                "                       ____/`---'\\____\n" +
                "                     .'  \\\\|     |//  `.\n" +
                "                    /  \\\\|||  :  |||//  \\\n" +
                "                   /  _||||| -:- |||||-  \\\n" +
                "                   |   | \\\\\\  -  /// |   |\n" +
                "                   | \\_|  ''\\---/''  |   |\n" +
                "                   \\  .-\\__  `-`  ___/-. /\n" +
                "                 ___`. .'  /--.--\\  `. . __\n" +
                "              .\"\" '<  `.___\\_<|>_/___.'  >'\"\".\n" +
                "             | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |\n" +
                "             \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /\n" +
                "        ======`-.____`-.___\\_____/___.-`____.-'======\n" +
                "                           `=---='\n" +
                "        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
                "                   佛祖保佑 永不宕机 永无BUG   ");
    }

    /**
     * 获取当前环境新
     *
     * @param environment 环境对象
     * @return
     */
    public String getCurrentProfile(Environment environment) {
        String[] activeProfiles = environment.getActiveProfiles();
        return activeProfiles.length == 0 ? "" : activeProfiles[0];
    }
}